package io.github.wslxm.springbootplus2.manage.sys.controller;

import io.github.wslxm.springbootplus2.client.sys.model.vo.USysDictGroupVO;
import io.github.wslxm.springbootplus2.client.sys.model.vo.USysDictVO;
import io.github.wslxm.springbootplus2.core.base.controller.BaseController;
import io.github.wslxm.springbootplus2.core.constant.BaseConstant;
import io.github.wslxm.springbootplus2.core.result.Result;
import io.github.wslxm.springbootplus2.manage.sys.model.dto.SysDictDTO;
import io.github.wslxm.springbootplus2.manage.sys.model.query.SysDictQuery;
import io.github.wslxm.springbootplus2.manage.sys.model.vo.SysDictVO;
import io.github.wslxm.springbootplus2.manage.sys.service.SysDictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 字典表
 *
 * @author wangsong
 * @WX-QQ 1720696548
 * @date Sun Nov 24 11:23:12 CST 2019
 */
@RestController
@RequestMapping(BaseConstant.Uri.API_ADMIN + "/sys/dictionary")
@Tag(name = "base--sys--字典管理")
public class SysDictController extends BaseController<SysDictService> {


    @GetMapping(value = "/tree")
    @Operation(summary = "列表查询 (默认返回Tree数据,可指定Tree或List)", description = "不能传递字符串数字Code查询")
    public Result<List<SysDictVO>> tree(@ModelAttribute SysDictQuery query) {
        return Result.success(baseService.tree(query));
    }

    @PostMapping
    @Operation(summary = "添加", description = "字符串类型的 Code不能重复,  数字类型的Code可以重复")
    public Result<String> insert(@RequestBody SysDictDTO dto) {
        return Result.successInsert(baseService.insert(dto));
    }


    @PutMapping(value = "/{id}")
    @Operation(summary = "编辑", description = "编辑后当前字典的字符串Code不能与其他字典的字符串Code重复， 不编辑Code时 + 编辑数字类型的Code时 不受影响")
    public Result<Boolean> upd(@PathVariable String id, @RequestBody SysDictDTO dto) {
        return Result.successUpdate(baseService.upd(id, dto));
    }


    @DeleteMapping(value = "/{id}")
    @Operation(summary = "ID删除", description = "删除当前ID数据以及该ID下的所有子层级数据")
    public Result<Boolean> del(@PathVariable String id) {
        return Result.successDelete(baseService.del(id));
    }


    @GetMapping(value = "/findCodeGroup")
    @Operation(summary = "查询所有-code分组", description = "1、根据Code字段分组排列数据,分组下的数据仍然有层级关系" +
            "\r\n 2、版本号(key=version)未发送变化后端不返回任何数据,前端请定义全局变量来缓存此字段" +
            "\r\n 3、所有select选择框,状态字段都使用此接口的数据获取中文值" +
            "\r\n 4、不包括禁用数据" +
            "\r\n 建议: 打开首页时调用此方法,刷新缓存数据, 刷新首页时在此刷新缓存"
    )
    public Result<Map<String, List<USysDictGroupVO>>> findCodeGroup() {
        return Result.successFind(baseService.findCodeGroup());
    }


    @GetMapping(value = "findByCode")
    @Operation(summary = "指定 code 查询字典列表", description = "通过字典code 查询当前字典下的所有可选数据 ")
    public Result<List<USysDictVO>> findByCode(@RequestParam(required = true) String code) {
        return Result.success(baseService.findByCode(code));
    }


    @GetMapping(value = "findByCodeNext")
    @Operation(summary = "指定 code + 字典值 code 查询指定字典", description = "")
    public Result<USysDictVO> findByCodeNext(@RequestParam(required = true) String code , @RequestParam(required = true)  String nextCode) {
        return Result.success(baseService.findByCodeNext(code,nextCode));
    }

    @GetMapping(value = "/generateEnum")
    @Operation(summary = "生成枚举", description = "" +
            "1、生成 Enum 的java 类和 生成js findCodeGroup查询数据的key\r\n" +
            "2、排序数字越小,越靠前\r\n" +
            "3、返回参数Map<String, String> ==>  map.java = 完整的java枚举字段  map.js = 代码枚举字典key,  前端直接通过key获取对应值\r\n" +
            "")
    public Result<Map<String, String>> generateEnum(String pid) {
        return Result.success(baseService.generateEnum(pid));
    }


}
